/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package run;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;



public class ServerStart{
    private static ServerSocket serverSocket = null;
   
    private static ServerPlayer serverPlayerList[];
    public static int countPlayer = 0;

    public static void main(String args[]) throws Exception{
        serverPlayerList = new ServerPlayer[4];

        serverSocket = new ServerSocket(4455);
        System.out.println("Server listening on port 4455");
        run();
    }

    public static void run(){
        // Chay luon MainScreen
        MainScreen.main(null);
        while (true){
            try{
                System.out.println("Waiting for connections:");
                Socket player = serverSocket.accept();
                System.out.println("Accepted a connection from:"+player.getInetAddress());
                ServerPlayer c = new ServerPlayer(player);
                serverPlayerList[countPlayer++] = c;
                //c.setVitri(countPlayer);
                
                /// Xu li message tu client
             //   BufferedReader inFromClient = new BufferedReader(new InputStreamReader(c.socket.getInputStream()));
             //   c.xuly2(inFromClient.readLine());

        /*        if (c.getVitri()==1){
                ServerScreen.setName1(c.getPlayerName());
                ServerScreen.setAnswer1(c.getAnswer());
               // ServerScreen.set
                }
                 else if (c.getVitri()==2){
                ServerScreen.setName2(c.getPlayerName());
                ServerScreen.setAnswer2(c.getAnswer());
                 }
                else if (c.getVitri()==3){
                ServerScreen.setName3(c.getPlayerName());
                ServerScreen.setAnswer3(c.getAnswer());
                 }
                else if (c.getVitri()==4){
                ServerScreen.setName4(c.getPlayerName());
                ServerScreen.setAnswer4(c.getAnswer());
                 }
                 */
                 }catch(Exception e){
                e.printStackTrace();
            }
        }
    }

    public static void sendNewMessage(String content) throws IOException{
        for (int i=0 ; i<countPlayer; i++)
            serverPlayerList[i].sendMessage(content);
    }

}